package com.vcredit.creditcat.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.ApiClazzEnum;
import com.vcredit.creditcat.framework.util.ConstantsUtil;
import com.vcredit.creditcat.framework.util.ExportImport;
import com.vcredit.creditcat.util.EnumExtUtil;
import com.vcredit.creditcat.vo.ChanneManVo;

/**
 * 导出
 *
 * @author chengxin
 *
 */
@Service
public class ExportService<T> extends BaseService {


	private int sysType=1;//0:win系统     1:linux系统

	/**
	 * 生成 Excel文件
	 *
	 * @param 集合
	 * @param 配置枚举名称
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> exportExcel(HttpServletRequest request, HttpServletResponse response, String className,
										   List<T>... rowBeanList) throws Exception {
		try {
			OutputStream out;
			ExportImport<T> exc = new ExportImport<T>();
			String excelName = "";

			String[] headers = null;
			ApiClazzEnum enumOnValue = EnumExtUtil.getEnumOnValue(ApiClazzEnum.class, className, "key");
			// 配置不同的类
			String title = ConstantsUtil.defaultSheet;
			switch (enumOnValue) {
				case feedback:
					// 表格名称
					excelName = ConstantsUtil.feedback;
					// 表头
					headers = ConstantsUtil.FeedbackHeaders;
					break;
				case channe:
					// 表格名称
					excelName = ConstantsUtil.channe;
					// 表头
					headers = ConstantsUtil.channeHeader;
					title = ConstantsUtil.channeTitleSheet;
			}

			// exce l 07   生成文件名
			String fileName = createfileName(excelName, ConstantsUtil.excel07);

			//生成文件地址
			String filepath = getUrl(fileName);


			// 生成Excel
			out = new FileOutputStream(new File(new String(filepath.getBytes(), "utf-8")));
			exc.createSheet(title, headers, out, enumOnValue.toString(), rowBeanList);
			Map<String, Object> recordIds = new HashMap<String, Object>();
			recordIds.put("fileName", filepath);
			// exc.downloads(filexlsx, response);
			return recordIds;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	//统一管理地址获取
	public String getUrl(String excelName){
		String path;
		if(sysType==0){//win
			path=ConstantsUtil.WinPath +"/"+ excelName;

			File folder = new File(ConstantsUtil.WinPath);
			if (!folder.exists()) {
				folder.mkdirs();
			}

		}else{
			path=ConstantsUtil.ExcelPath +"/"+ excelName;

			File folder = new File(ConstantsUtil.ExcelPath);
			if (!folder.exists()) {
				folder.mkdirs();
			}
		}


		return  path;
	}

	public void downExcel(HttpServletResponse response, String path) throws Exception {
		ExportImport<ChanneManVo> exc = new ExportImport<ChanneManVo>();
		exc.downloads(path, response);
	}

}
